package com.yc.rh.actions;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.interceptor.ServletRequestAware;

import com.opensymphony.xwork2.ActionSupport;
import com.yc.rh.utils.SessionAttributeNames;
import com.yc.rh.biz.IAdminInfoBiz;
import com.yc.rh.entities.AdminInfo;
import com.yc.rh.entities.JsonObject;

public class AdminInfoAction extends ActionSupport implements ServletRequestAware{
	private static final long serialVersionUID = -5036413065824317285L;
	
	private HttpServletRequest request;
	private HttpSession session;
	private JsonObject<AdminInfo> jsonObject;
	private IAdminInfoBiz adminInfoBiz;
	private AdminInfo adminInfo;
	
	public JsonObject<AdminInfo> getJsonObject() {
		return jsonObject;
	}

	public void setJsonObject(JsonObject<AdminInfo> jsonObject) {
		this.jsonObject = jsonObject;
	}

	public IAdminInfoBiz getAdminInfoBiz() {
		return adminInfoBiz;
	}

	public void setAdminInfoBiz(IAdminInfoBiz adminInfoBiz) {
		this.adminInfoBiz = adminInfoBiz;
	}

	public void setAdminInfo(AdminInfo adminInfo) {
		this.adminInfo = adminInfo;
	}

	public AdminInfo getAdminInfo() {
		return adminInfo;
	}

	@Override
	public void setServletRequest(HttpServletRequest arg0) {
		this.request=arg0;
		this.session=request.getSession();
	}
	
	/**
	 * 后台查询所有用户信息
	 * @return
	 */
	public String getAdminAll(){
		int page=Integer.parseInt(request.getParameter("page"));
		int rows=Integer.parseInt(request.getParameter("rows"));
		
//		session=request.getSession();
//		System.out.println("session  "+session.getAttribute("user"));
		
		jsonObject.setRows(adminInfoBiz.find(page, rows));
		jsonObject.setTotal(adminInfoBiz.getTotal());
		return "success";
	}
	
	/**
	 * 添加管理员信息
	 * @param request
	 * @param response
	 */
	public String addAdminInfo() {
		jsonObject.setResult(adminInfoBiz.addAdminInfo(adminInfo));
		return "success";
	}
	
	/**
	 * 删除管理员
	 * @param request
	 * @param response
	 */
	public String delAdminInfo() {
		String aids=request.getParameter("aids");
		jsonObject.setResult(adminInfoBiz.delAdminInfo(aids));
		return "success";
	}
	
	/**
	 * 修改管理员信息
	 * @param request
	 * @param response
	 */
	public String updateAdminInfo() {
		jsonObject.setResult(adminInfoBiz.updateAdmin(adminInfo));
		return "success";
	}

	/**
	 * 管理员用户登录
	 * @param request
	 * @param response
	 */
	public String adminLogin() {
		String aid=request.getParameter("aid");
		String pwd=request.getParameter("pwd");
		
		AdminInfo adminInfo=adminInfoBiz.loginAdmin(aid, pwd);
		session.setAttribute(SessionAttributeNames.CURRENTADMININFO, adminInfo);
		if(adminInfo!=null){
			jsonObject.setResult(1);
		}else{
			jsonObject.setResult(0);
		}
		return "success";
	}
}
